<?php
/**
 * 
 * 道具 处理类
 * @author tanzhe
 *
 */
class ItemModel {
	private $db;
	private $cache;
	
	public function __construct() {
		$this->db = new Data();
		$this->cache = new Cache();
	}
	
	public function getItem($key, $shopId) { //获得商店信息
	 	$playerInfo = $this->cache->get('ku_'.$key);
		$chkRet = Common::checkPlayerInfo($playerInfo);
		if ($chkRet != 0) {
			return array('code'=>$chkRet,'ret'=>'');
		}
		$itemList = $this->cache->get('itemlist_'.$shopId);
		if ( empty($itemList) ) {
			$query = $this->db->query("select `label` from item where `shop_id`='{$shopId}' order by id asc");
			$data = $this->db->fetchAllByQuery($query);
			$itemList = array();
			foreach ( $data as $v ) {
				$itemList[] = $v['label'];
			}
			$this->cache->set('itemlist_'.$shopId, $itemList);
		}
		return array('code'=>0,'ret'=>array('itemlist'=>$itemList));
	}

	/**
	 * 
	 * 使用道具
	 * @param str $key
	 * @param str $label 道具标识
	 */
	function useItem($key, $label) {
		$playerInfo = $this->cache->get('ku_'.$key);
		$chkRet = Common::checkPlayerInfo($playerInfo);
		if ($chkRet != 0) {
			return array('code'=>$chkRet,'ret'=>'');
		}
		if ( !empty($label) ) {
			$itemData = $this->cache->get('itemdata_'.$label);
			if ( empty($itemData) ) {
				$query = $this->db->query("select * from item where `label`='{$label}'");
				$itemData = $this->db->fetchOneByQuery($query);
				$this->cache->set('itemdata_'.$label, $itemData);
			}
			if ( empty($itemData) ) { //不存在该道具
				return array('code'=>213,'ret'=>'');
			}
		} else {
			return array('code'=>213,'ret'=>'');
		}
		
		//判断钱是否足够
		if ( $itemData['cost_type'] == 1 ) {
			if ( $itemData['price'] > $playerInfo['coin'] ) {
				return array('code'=>9,'ret'=>'');
			}
		} else {
			if ( $itemData['price'] > $playerInfo['cash'] ) {
				return array('code'=>10,'ret'=>'');
			}
		}
		
		//判断叠加限制 预留
		$time =time();
		$updateData = array();
		//道具作用
		switch ( $itemData['type'] ) {
			case 3: //增加当前体力值
				if( $playerInfo['now_energy'] >= $updateData['max_energy'] ) {
					return array('code'=>215,'ret'=>''); 
				}
				$updateData['now_energy'] = $playerInfo['now_energy'] + $itemData['bonus'];
				if ( $updateData['now_energy'] > $playerInfo['max_energy'] ) {
					$updateData['now_energy'] = $playerInfo['max_energy'];
				}
			break;
			case 4: //增加最大体力值
				$updateData['max_energy'] = $playerInfo['max_energy'] + $itemData['bonus'];
				//重新开始算cd时间
				if( ($playerInfo['now_energy'] < $updateData['max_energy']) && ($playerInfo['now_energy'] >= $playerInfo['max_energy']) ) {
					$playerInfo['last_energy_time'] = $time;
					global $fillEnergyCdTime;
					$playerInfo['next_energy_time'] = $time + floor($fillEnergyCdTime/(1+$playerInfo['cd_factor']/100));
				}
			break;
			case 5:
				$this->addBuff($label, $itemData['bonus'], $itemData['last_time'], $playerInfo);
			break;
			default:
				return array('code'=>214,'ret'=>''); 
			break;
		}
		
		if ( $itemData['cost_type'] == 1 ) {
			$updateData['coin'] = $playerInfo['coin'] - $itemData['price'];
		} else {
			$updateData['cash'] = $playerInfo['cash'] - $itemData['price'];
		}
		//能量满时消耗掉后开始恢复
		$countDown = 0;
		if ( $playerInfo['now_energy'] < $playerInfo['max_energy'] ) {
			$countDown = $playerInfo['next_energy_time'] - $time;
		}
		
		$playerInfo = array_merge($playerInfo, $updateData);
		//更新角色信息
		$options = array(
			'key'=>array(
				'u_id'=>$playerInfo['u_id'],
			),
			'data'=>$updateData,
		);
		$this->db->updateData('player_info', $options);
		$this->cache->set("ku_".$key, $playerInfo);
		//写元宝log
		if ( isset($updateData['cash']) ) {
			$aryData = array(
				'label'=>$label,
			);
			$options = array(
				'u_id'=> $playerInfo['u_id'],
				'type'=> 'dojob',
				'num'=> -$itemData['price'],
				'data'=> serialize($aryData),
				'add_time'=> time(),
			);
			Common::cashLog($options);
		}
		//返回个人信息
		$ret = array(
			'coin'=>$playerInfo['coin'],
			'cash'=>$playerInfo['cash'],
			'now_energy'=>$playerInfo['now_energy'],
			'max_energy'=>$playerInfo['max_energy'],
			'hanger'=>$playerInfo['hanger'],
			'count_down'=>$countDown,
			'coin_factor'=>$playerInfo['coin_factor'],
			'hanger_factor'=>$playerInfo['hanger_factor'],
			'cd_factor'=>$playerInfo['cd_factor'],
		);
		return array('code'=>0,'ret'=>$ret);
	}
	
	/**
	 * 
	 * 增加buff
	 * @param str $label 道具标识
	 * @param str $bonus 内容 array('label1'=>value1,'label2'=>value2)的序列化
	 * @param int $lastTime 持续时长
	 * @param array $playerInfo 角色信息
	 */
	function addBuff($label, $bonus, $lastTime ,&$playerInfo) {
		if ( empty($bonus) ) {
			return false;
		}
		$startTime = time();
		$endTime = empty($lastTime) ? 0 : $startTime + $lastTime;
		$updateData = array();
		$aryBonus = empty($bonus) ? array() : unserialize($bonus);
		$oldCdFactor = $playerInfo['cd_factor'];
		foreach ( $aryBonus as $k=>$v ) {
			if ( !empty($v) ) {
				if ( $k == 'add_hanger' ) {
					$updateData['hanger'] = $playerInfo['hanger'] + $v;
					$updateData['hanger_factor'] = $playerInfo['hanger_factor'] + $v;
					$playerInfo['hanger'] = $updateData['hanger'];
					$playerInfo['hanger_factor'] = $updateData['hanger_factor'];
				} elseif ( $k == 'add_coin' ) {
					$updateData['coin_factor'] = $playerInfo['coin_factor'] + $v;
					$playerInfo['coin_factor'] = $updateData['coin_factor'];
				} elseif ( $k == 'reduce_cd' ) {
					$updateData['cd_factor'] = $playerInfo['cd_factor'] + $v;
					$playerInfo['cd_factor'] = $updateData['cd_factor'];
				}
			}
		}
		if ( count($updateData) == 0 ) {
			return false;
		}
		if ( isset($updateData['cd_factor']) && ($playerInfo['now_energy'] < $playerInfo['max_energy']) ) { //重新计算next_energy_time
			//预计时间 = 当前时间 + 剩余所需时间
			$cdFactor = (1 + $playerInfo['cd_factor']/100) / (1 + $oldCdFactor/100);
			$updateData['next_energy_time'] = $startTime + ceil(($playerInfo['next_energy_time'] - $startTime) / $cdFactor);
			$playerInfo['next_energy_time'] = $updateData['next_energy_time'];
		}
		$options = array(
			'key'=>array(
				'u_id'=>$playerInfo['u_id'],
			),
			'data'=>$updateData,
		);
		$this->db->updateData('player_info', $options);
		$this->cache->set("ku_".$playerInfo['acc_key'], $playerInfo);
		$insertData = array(
			'u_id'=>$playerInfo['u_id'],
			'item_label'=>$label,
			'start_time'=>$startTime,
			'end_time'=>$endTime,
			'bonus'=>$bonus,
		);
		$this->db->insertData('player_buff', $insertData);
		return true;
	}
	
	public function endBuff($key) { //查看是否存在过期buff
		$playerInfo = $this->cache->get('ku_'.$key);
		$chkRet = Common::checkPlayerInfo($playerInfo);
		if ($chkRet != 0) {
			return array('code'=>$chkRet,'ret'=>'');
		}
		$time = time();
		$sql = " select `id`,`bonus`,`end_time` from player_buff where `u_id`={$playerInfo['u_id']} and `end_time`!=0";
		$query = $this->db->query($sql);
		if ($this->db->num_rows($query)==0) { //没有过期道具
			return array('code'=>0,'ret'=>'');
		}
		$buff = $this->db->fetchAllByQuery($query);
		$nextTime = 0;
		foreach ( $buff as $v ) {
			if ( $v['end_time'] < $time  ) {
				$this->removeBuff($v['bonus'], $playerInfo);
				$deleteSql = "delete from player_buff where id={$v['id']} limit 1";
				$this->db->query($deleteSql);	
			} else { //获取下次请求时间
				$remainTime = $v['end_time'] - $time;
				if ( $nextTime == 0 ) {
					$nextTime = $remainTime;
				}else{
					$nextTime = $nextTime > $remainTime ? $remainTime : $nextTime;
				}
			}
		}
		$countDown = 0;
		if ( $playerInfo['now_energy'] < $playerInfo['max_energy'] ) {
			$countDown = $playerInfo['next_energy_time'] - $time;
		}
		//返回个人信息
		$ret = array(
			'now_energy'=>$playerInfo['now_energy'],
			'max_energy'=>$playerInfo['max_energy'],
			'hanger'=>$playerInfo['hanger'],
			'count_down'=>$countDown,
			'coin_factor'=>$playerInfo['coin_factor'],
			'hanger_factor'=>$playerInfo['hanger_factor'],
			'cd_factor'=>$playerInfo['cd_factor'],
			'next_time'=>$nextTime,
		);
		return array('code'=>0,'ret'=>$ret);
	}
	
	/**
	 * 
	 * 移除buff
	 * @param str $label 道具标识
	 * @param str $bonus 内容 array('label1'=>value1,'label2'=>value2)的序列化
	 * @param array $playerInfo 角色信息
	 */
	function removeBuff($bonus, &$playerInfo) {
		if ( empty($bonus) ) {
			return false;
		}
		$startTime = time();
		$updateData = array();
		$aryBonus = empty($bonus) ? array() : unserialize($bonus);
		$oldCdFactor = $playerInfo['cd_factor'];
		foreach ( $aryBonus as $k=>$v ) {
			if ( !empty($v) ) {
				if ( $k == 'add_hanger' ) {
					$updateData['hanger'] = $playerInfo['hanger'] - $v;
					$updateData['hanger_factor'] = $playerInfo['hanger_factor'] - $v;
					$playerInfo['hanger'] = $updateData['hanger'];
					$playerInfo['hanger_factor'] = $updateData['hanger_factor'];
				} elseif ( $k == 'add_coin' ) {
					$updateData['coin_factor'] = $playerInfo['coin_factor'] - $v;
					$playerInfo['coin_factor'] = $updateData['coin_factor'];
				} elseif ( $k == 'reduce_cd' ) {
					$updateData['cd_factor'] = $playerInfo['cd_factor'] - $v;
					$playerInfo['cd_factor'] = $updateData['cd_factor'];
				}
			}
		}
		if ( count($updateData) == 0 ) {
			return false;
		}
		if ( isset($updateData['cd_factor']) && ($playerInfo['now_energy'] < $playerInfo['max_energy']) ) { //重新计算next_energy_time
			//预计时间 = 当前时间 + 剩余所需时间
			$cdFactor = (1 + $playerInfo['cd_factor']/100) / (1 + $oldCdFactor/100);
			$updateData['next_energy_time'] = $startTime + ceil(($playerInfo['next_energy_time'] - $startTime) / $cdFactor);
			$playerInfo['next_energy_time'] = $updateData['next_energy_time'];
		}
		$options = array(
			'key'=>array(
				'u_id'=>$playerInfo['u_id'],
			),
			'data'=>$updateData,
		);
		$this->db->updateData('player_info', $options);
		$this->cache->set("ku_".$playerInfo['acc_key'], $playerInfo);
		return true;
	}
}
?>